import { login } from '@/api/user'
import { getToken, getUserInfo, removeToken, removeUserInfo, setToken, setUserInfo } from '@/utils/auth'
import { Message } from 'element-ui'
// import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    userInfo: JSON.parse(getUserInfo() ? getUserInfo() : null)
  }
}

const state = getDefaultState()

const mutations = {
  setToken(state, value) {
    state.token = value
  },
  setUserInfo(state, value) {
    state.userInfo = value
  },
  logout(state, value) {
    state.token = ''
    state.userInfo = {}
    removeToken()
    removeUserInfo()
  }
}

const actions = {
  async doToken(context, value) {
    const res = await login(value)
    console.log(res)
    setToken(res.data.data.token.token)
    setUserInfo(res.data.data.user)
    context.commit('setToken', res.data.data.token.token)
    context.commit('setUserInfo', res.data.data.user)
    Message.success('登录成功')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

